/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {

        return GetMinDepth(root);

    }

    int GetMinDepth(TreeNode* node)
    {
        if (node == NULL)
            return 0;

        int leftHeight = GetMinDepth(node->left);
        int rightHeight = GetMinDepth(node->right);

        if (node->left == NULL && node->right != NULL)
        {
            return 1 + rightHeight;
        }

        if (node->left != NULL && node->right == NULL)
        {
            return 1 + leftHeight;
        }


        int res = min(leftHeight, rightHeight) + 1;

        return res;
    }
};